//
//  main.m
//  OC05_homework
//
//  Created by Hanlingyu on 16/7/8.
//  Copyright © 2016年 ltq. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Person.h"
int main(int argc, const char * argv[]) {
    
//    一、
//    1.创建1个包含5个字符串元素的数组；
//    2.用for循环倒序输出所有元素；
//    3.用for...in完成所有字符串元素的拼接(生成一个新的字符串)，并输出拼接好的字符串。
    NSLog(@"dsfads");
    NSArray *stringArray = [NSArray arrayWithObjects:@"5", @"1", @"3", @"2", @"1", nil];
    for (NSInteger i = stringArray.count - 1; i >= 0; i--) {
        NSString *string = stringArray[i];
        NSLog(@"%@", string);
    }
    
    NSMutableString *totalString = [NSMutableString string];
    for (NSString *string in stringArray) {
        [totalString appendString:string];
    }
    NSLog(@"%@", totalString);
    
    
    
//    二、
//    1.创建1个包含5个person对象的数组(姓名用英文)；
//    2.用for循环找到年龄最大的Person对象；
//    3.用for...in遍历数组，把大于某个年龄的Person对象的姓名后面拼接上字符串“-brother”。
    
    Person *per1 = [[Person alloc] initWithName:@"liutanqi" sex:@"男" age:23];
    Person *per2 = [[Person alloc] initWithName:@"hanlingyu" sex:@"女" age:22];
    Person *per3 = [[Person alloc] initWithName:@"zhangjian" sex:@"男" age:24];
    Person *per4 = [[Person alloc] initWithName:@"luchang" sex:@"男" age:34];
    Person *per5 = [[Person alloc] initWithName:@"liusiyu" sex:@"女" age:32];

    NSArray *personArray = @[per1, per2, per3, per4, per5];
    Person *olderPer = [[Person alloc] init];
    olderPer.age = 0;
    for (NSInteger i = 0; i < personArray.count; i++) {
        Person *per = personArray[i];
        if (olderPer.age < per.age) {
            olderPer = per;
        }
    }
    NSLog(@"%@", olderPer);
    
  // 大于30岁的女生, 名字后面加sister, 大于30岁的男生, 后面加brother
    for (Person *per in personArray) {
        if (per.age > 30) {
            if ([per.sex isEqualToString:@"男"]) {
                per.name = [NSString stringWithFormat:@"%@-brother", per.name];
            } else {
                per.name = [NSString stringWithFormat:@"%@-sister", per.name];
            }
        }
        NSLog(@"%@", per);
    }

    
//    三、
//    1.使用第二题目中生成的数组；
//    2.使用sortUsingSelector: 分别按年龄，姓名，性别进行排序，输出排序结果；
//    3.使用NSSortDescriptor，按姓名进行排序。
    
    
    personArray = [personArray sortedArrayUsingSelector:@selector(compareByAge:)];
    NSLog(@"按年龄排序: %@", personArray);
    personArray = [personArray sortedArrayUsingSelector:@selector(compareByName:)];
    NSLog(@"按名字排序: %@", personArray);
    personArray = [personArray sortedArrayUsingSelector:@selector(compareBySex:)];
    NSLog(@"按性别排序: %@", personArray);
    
    
    NSSortDescriptor *perSort = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
    personArray = [personArray sortedArrayUsingDescriptors:@[perSort]];
    NSLog(@"%@", personArray);
    
    
//    四、
//    处理省市区文件中的area.txt
//    将省市区进行分割处理, 省里包含市, 市里包含区
//    1. 先用数组字典表示
//    2. 将数组字典的处理方式转换成数组模型(Class)
    
    NSString *path = @"/Users/ltq/DLS160710/Objective-C/HomeWork/OC05_homework/OC05_homework/area.txt";
    // 通过文件路径, 读取文件中的字符串
    NSString *sourceString = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
    // 根据\n切割字符串
    NSArray *strArray = [sourceString componentsSeparatedByString:@"\n"];
//    NSLog(@"%@", strArray);
    
    NSMutableArray *proArr = [NSMutableArray array];

    NSString *proviceName;
    NSString *cityName;
    for (NSString *lineStr in strArray) {
        if (![lineStr hasPrefix:@" "]) {
            NSMutableArray *cityArr = [NSMutableArray array];
            NSMutableDictionary *proDic = [NSMutableDictionary dictionaryWithObjectsAndKeys:lineStr, cityArr, nil];
            [proArr addObject:proDic];
            proviceName = lineStr;
        }
        else if (![lineStr hasPrefix:@"    "]) {
            NSMutableArray *areaArr = [NSMutableArray array];
            NSMutableDictionary *cityDic = [NSMutableDictionary dictionaryWithObjectsAndKeys:lineStr, areaArr, nil];
            /** 将省数组的最后一个值赋给省字典. */
            NSMutableDictionary *proDic = proArr.lastObject;
            /** 将省字典的Value赋给城市数组. */
            NSMutableArray *cityArr = [proDic objectForKey:proviceName];
            [cityArr addObject:cityDic];
            cityName = lineStr;
            
        }
        else {
            NSMutableDictionary *proDic = proArr.lastObject;
            NSMutableArray *cityArr = [proDic objectForKey:cityName];
            NSMutableDictionary *cityDic = cityArr.lastObject;
            NSMutableArray *areaArr = [cityDic objectForKey:@"area"];
            [areaArr addObject:lineStr];
            
        }
        
    }

   // NSLog(@"%@", proArr);
    
    
    
//    五、拓展
//    上述作业做完, 处理Location1.txt
//    要求同第四题。自己寻找规律
    
    NSString *path1 = @"/Users/ltq/DLS160710/Objective-C/HomeWork/OC05_homework/OC05_homework/Location1.txt";
    NSString *dataString1 = [NSString stringWithContentsOfFile:path1 encoding:NSUTF8StringEncoding error:nil];
    NSArray *dataArray1 = [dataString1 componentsSeparatedByString:@","];
    dataArray1 = [dataArray1 sortedArrayUsingComparator:^NSComparisonResult(NSString*  _Nonnull obj1, NSString*  _Nonnull obj2) {
        NSArray *arr1 = [obj1 componentsSeparatedByString:@"|"];
        NSArray *arr2 = [obj2 componentsSeparatedByString:@"|"];
        int aNum = [arr1[1] intValue];
        int bNum = [arr2[1] intValue];
        if (aNum > bNum) {
            return NSOrderedDescending;
        } else if (aNum < bNum) {
            return NSOrderedAscending;
        } else {
            return NSOrderedSame;
        }
    }];
    
    NSMutableArray *country = [NSMutableArray array];
    NSString *province;
    NSString *city1;
    for (NSString *line in dataArray1) {
        if ([line hasSuffix:@"0000"]) {
            NSMutableDictionary *dic = [NSMutableDictionary dictionary];
            NSMutableArray *arr = [NSMutableArray array];
            [dic setObject:arr forKey:line];
            [country addObject:dic];
            province = line;
        } else if ([line hasSuffix:@"00"]){
            NSMutableDictionary *dic = [NSMutableDictionary dictionary];
            NSMutableArray *arr = [NSMutableArray array];
            [dic setObject:arr forKey:line];
            [[[country lastObject] objectForKey:province] addObject:dic];
            city1 = line;
        } else {
            [[[[[country lastObject] objectForKey:province] lastObject] objectForKey:city1] addObject:line];
        }
    }
    
    
    NSLog(@"%@",country);
    
    
    return 0;
}
